﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Transactions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineTestBLL;
using OnlineTestModel;
using FineUI;
using Unit = OnlineTestModel.Unit;


namespace OnlineTestUI.BackManager
{
    public partial class addpaper : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            PartsBLL pb = new PartsBLL();
            // 从数据库返回数据表
            
            DataSet ds =  pb.GetAllList();
            
            ds.Relations.Add("TreeRelation", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["parentid"]);
            DataTable table = ds.Tables[0];
            foreach (DataRow row in table.Rows)
            {
                if (Convert.ToInt32(row["partnumber"])%100==0)
                {
                    FineUI.TreeNode node = new FineUI.TreeNode();
                    node.Text = "第"+(Convert.ToInt32(row["partnumber"])/100)+"章  "+row["partname"].ToString();
                    node.Expanded = true;
                    node.AutoPostBack = true;
                    node.EnableCheckBox = true;
                    node.NodeID = row["partnumber"].ToString();
                    treePointList.Nodes.Add(node);
                    ResolveSubTree(row, node);
                }
            }
        }

        private void ResolveSubTree(DataRow dataRow, FineUI.TreeNode treeNode)
        {
            DataRow[] rows = dataRow.GetChildRows("TreeRelation");
            if (rows.Length > 0)
            {
                treeNode.Expanded = true;
                foreach (DataRow row in rows)
                {
                    FineUI.TreeNode node = new FineUI.TreeNode();
                    node.Text = "第" + (Convert.ToInt32(row["partnumber"]) % 100) + "节  " + row["partname"].ToString();
                    node.NodeID = row["partnumber"].ToString();
                    node.EnableCheckBox = true;
                    treeNode.Nodes.Add(node);
                    ResolveSubTree(row, node);
                }
            }
        }
        protected void TreePointList_NodeCheck(object sender, FineUI.TreeCheckEventArgs e)
        {
            if (e.Checked)
            {
                treePointList.CheckAllNodes(e.Node.Nodes);
            }
            else
            {
                treePointList.UncheckAllNodes(e.Node.Nodes);
            }

     }
        protected void BtAdd_Click(object sender, EventArgs e)
        {
            string paperName = tbPapername.Text;
            string description = tbDescription.Text;
            string status = tbStatus.Text;
            int choiceNum = Convert.ToInt32(tbChoiceNum.Text);
            int choiceScore = Convert.ToInt32(tbChoiceScore.Text);
            int completionNum = Convert.ToInt32(tbCompletionNum.Text);
            int completionScore = Convert.ToInt32(tbCompletionScore.Text);
            int tofNum = Convert.ToInt32(tbTofNum.Text);
            int tofScore = Convert.ToInt32(tbTofScore.Text);
            int shortNum = Convert.ToInt32(tbShortNum.Text);
            int shortScore = Convert.ToInt32(tbShortScore.Text);
            double kpCoverage = Convert.ToDouble(tbKPCoverage.Text);
            double difficulty = Convert.ToDouble(tbDifficulty.Text);
            int courseId = 1;
             FineUI.TreeNode[] nodes = treePointList.GetCheckedNodes();
            List<int> pointsList = new List<int>();
            if (nodes.Length > 0)
            {
                
                foreach (FineUI.TreeNode node in nodes)
                {
                    pointsList.Add(Convert.ToInt32(node.NodeID));
                }
                
            }
            
            PapersBLL pb = new PapersBLL();
            PaperQuestionsBLL pqb = new PaperQuestionsBLL();
            CreatePaper cp = new CreatePaper(kpCoverage,difficulty);
            OnlineTestModel.Unit unit=cp.Create(0.96, choiceNum, completionNum, tofNum, shortNum, pointsList, choiceScore, completionScore,
                      tofScore, shortScore, courseId);
            Papers p = new Papers();
            p.PaperName = paperName;
            p.Description = description;
            p.CreateTime = DateTime.Now;
            //p.CreateId = ((Teachers) Session["Teachers"]).Id;
            p.CreateId = 1;
            p.Status = "1,"+rblStatus.SelectedValue+",0";
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    int result =pb.Add(p);
                    if (result > 0)
                    {
                        
                        for (int i = 0; i < unit.ProblemList.Count; i++)
                        {
                            QuestionEx qe = unit.ProblemList[i];
                            PaperQuestions pq = new PaperQuestions();
                            pq.PaperId = result;
                            pq.QuestionId = qe.Id;
                            pq.Score = qe.Score;
                            switch (qe.Type)
                            {
                                case 1:
                                    pq.BigQuestion = 1;
                                    break;
                                case 2:
                                    pq.BigQuestion = 2;
                                    break;
                                case 3:
                                    pq.BigQuestion = 3;
                                    break;
                                case 4:
                                    pq.BigQuestion = 4;
                                    break;
                            }
                            //此时不能确定小题号，只好之后修改试卷时再写入
                            pq.SmallQuestion = 0;
                            pqb.Add(pq);

                        }
                        ts.Complete();


                        Alert.Show("添加成功");
                        PageContext.Redirect("editpaper.aspx?id="+result+"","_self");

                    }
                    else
                    {
                        Alert.Show("出现错误");
                    }

                }

            }
            catch (Exception)
            {
                Alert.Show("出现错误");
            }
        }
    }
}